﻿// #
// #
// # Canvas Object is a pre-object for all canvas-placed objects (as opposed to dashboard objects)
// # Here rules for ALL canvasObjects can be defined so optimizing can take place.
// # CanvasObject extends MovieClip and provides all MovieClip assets that are needed
// #

/* 
	This still needs to reworked,
	but the most important function of this class
	is that it will take any object placed on the canvas
	and handle visible, invisible settings and such
*/


package com.sense.canvas {
	
	import flash.display.*
	import flash.events.*
	import flash.events.EventDispatcher
	import flash.net.*
	import flash.text.*
	import flash.geom.*
	import flash.utils.getQualifiedClassName
	
	import fl.transitions.*
	import fl.transitions.easing.*
	import flash.utils.Timer
	
	import com.sense.canvas.*
	import com.sense.utils.ReloadButton
	
	public class CanvasObject extends MovieClip {
		
		public var count // for wipe in
		public var _currentClass
		public var myReloadButton 
		public var rb
		public var reAddTimer:Timer
		
		public function CanvasObject(assignedClass = null):void {
			trace("CanvasObject Created")
			_currentClass = assignedClass
			trace("CanvasObject assigned Class: ", assignedClass)
			addEventListener(Event.ADDED_TO_STAGE, addToStage)
			addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage)

			if (assignedClass != null) {
				// buildReloadButton()
			}
		}
		
		public function buildReloadButton() {
			reAddTimer = new Timer(5000, 1)
			reAddTimer.addEventListener(TimerEvent.TIMER, function(e:Event) { addChild(myReloadButton) } )

			myReloadButton = new ReloadButton(_currentClass)
			myReloadButton.addEventListener(Event.REMOVED_FROM_STAGE, function(e:Event) { reAddTimer.start() } )  
			myReloadButton.y = -50
			addChild(myReloadButton)
		}

		public function testLocation(e:Event) {
			
			/*
			### This is a very peculiar problem,
			### as not all canvas objects have the same root x and y (some are part of a larger system)
			### these calculations are weird, there has to be a way to calculate if an object (any object)
			### is currently visible on the screen or not;
			### but for now I don't seem to be managing it.
			### further research needed
			
			### THIS WORKS ONLY WITH CANVASOBJECTS WITH A > 0, > 0 REFERENCE FROM LOADBUILD 
			*/
			
			/*
			if (getQualifiedClassName(this) == "nl.nltracks.relatedcontent::RelatedContent") {
				trace("testing location:", position)
				trace("testing location globaltolocal:", globalPos)
				trace("testing location: localtoglobal", localPos)
				trace("testing canvas location:", canvasPos)
				trace("testing width:", localPos.x + canvasPos.x )
				trace("testing height:", localPos.y + canvasPos.y )
				trace(visible)
			}
			*/

			//var globalRef = stage.getChildByName("globals")
			//var canvasRef = globalRef.Controller.myCanvas
			//var canvasPos:Point = new Point(canvasRef.x, canvasRef.y)
			
			try {
				var position:Point = new Point(x, y)
				var localPos:Point = localToGlobal(position)
				var globalPos:Point = globalToLocal(position)
			
				if ( position.x - (globalPos.x  - this.width) < 0  ) {
					visible = false
				}else if ( position.x - globalPos.x > stage.stageWidth ) {
					visible = false
				}else if ( position.y - (globalPos.y - this.height) < 0 ) {
					visible = false
				}else if ( position.y - globalPos.y > stage.stageHeight ) {
					visible = false
				}else{
					visible = true
				}
			} catch(e) {
				trace("canvas object failed ... ", this, "\n", e)
			}
			
			// trace(visible)
		}

		public function disableVisibleToggle() {
			removeEventListener(Event.ADDED_TO_STAGE, addToStage)
			removeEventListener(Event.REMOVED_FROM_STAGE, removeFromStage)
		}
		
		public function addToStage(e:Event) {
			addEventListener(Event.ENTER_FRAME, testLocation)
		}
		
		public function removeFromStage(e:Event) {
			removeEventListener(Event.ENTER_FRAME, testLocation)
		}
	}
}